package leetcodev1.字符串;

import leetcodev1.链表.Solution;

public class LeetCode165 {

    public static void main(String[] args) {
        String str = "01";
        System.out.println(Integer.valueOf(str));
    }

    public int compareVersion(String version1, String version2) {
        String[] version1Detail = version1.split("\\.");
        String[] version2Detail = version2.split("\\.");
        int length1 = version1Detail.length, length2 = version2Detail.length, index = 0;
        while (index < length1 || index < length2) {
            int version1Num = index < length1 ? Integer.parseInt(version1Detail[index]) : 0;
            int version2Num = index < length2 ? Integer.parseInt(version2Detail[index]) : 0;

            if (version1Num > version2Num) {
                return 1;
            } else if (version1Num < version2Num) {
                return -1;
            }
            index++;
        }
        return 0;
    }

    //切分的同时进行比较
    public int compareVersion1(String version1, String version2) {
        int n = version1.length(), m = version2.length();
        int i = 0, j = 0;
        while (i < n || j < m) {
            int x = 0;
            for (; i < n && version1.charAt(i) != '.'; ++i) {
                x = x * 10 + version1.charAt(i) - '0';//每次扩充10倍后++ 即使是0也无所谓
            }
            ++i; // 跳过点号
            int y = 0;
            for (; j < m && version2.charAt(j) != '.'; ++j) {
                y = y * 10 + version2.charAt(j) - '0';
            }
            ++j; // 跳过点号
            //立即比较 相同则继续向下
            if (x != y) {
                return x > y ? 1 : -1;
            }
        }
        return 0;
    }

}
